package com.winchain.chainbill.bill.biz.send.cmd;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.service.AcctTxRecService;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.command.CommandContext;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillSndTranCmd extends TxCmd<CbSnd> {
	@Autowired
	private AcctTxRecService AcctTxRecService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public boolean execute(CommandContext<Bill<CbSnd>> context) throws Exception {
		boolean timeout = ((Boolean) context.getValue("Timeout")).booleanValue();
		if (!timeout) {
			AcctTxRec tran = getAcctTxRec(context);
			this.AcctTxRecService.save(tran);

			context.setValue("acctTxRec", tran);
		}
		return false;
	}

	private AcctTxRec getAcctTxRec(CommandContext<Bill<CbSnd>> context) {
		CbRec record = ((Bill) context.getSource()).getRec();

		CbSnd send = (CbSnd) ((Bill) context.getSource()).getSource();
		String billId = record.getBillId();
		String finId = record.getFinId();
		String buyerId = send.getBuyerId();
		BigDecimal billAmt = record.getBillAmt();
		String abs = "������������";

		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(SystemUtil.getUUID());

		String acctUse = "1";
		AcctBas creditAcct = getAcctBas(buyerId, acctUse, null);

		txRec.setTxAcctNo(creditAcct.getAcctNo());
		txRec.setTxAcctNm(creditAcct.getAcctNm());
		txRec.setTxBankNm(creditAcct.getFiNm());

		txRec.setTxAmt(billAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxType("cb_snd");
		txRec.setOptType("qt_use");

		txRec.setTxSts("I");

		acctUse = "2";
		AcctBas acct = getAcctBas(finId, acctUse, null);
		txRec.setCpAcctNo(acct.getAcctNo());
		txRec.setCpAcctNm(acct.getAcctNm());
		txRec.setCpBankNm(acct.getFiNm());
		txRec.setCurrCode(AssetCode.XQT.name());
		txRec.setTxChnCode("C");
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(billId);
		txRec.setDataScLvl("B");

		AcctBkp txAcctBkp = this.billAcctBlcService.getCreditAcctBkpByCont(finId, buyerId);
		txRec.setTxBkpId(txAcctBkp.getBkpId());
		AcctBkp cpAcctBkp = this.billAcctBlcService.getAcctBkp(txRec.getCpAcctNo(), txRec.getCurrCode(), null, null);
		txRec.setCpBkpId(cpAcctBkp.getBkpId());
		return txRec;
	}
}
